﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Home.Haushalt.Lib.Element;
using System.IO;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmDataIntegrityCheck : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmDataIntegrityCheck()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDataIntegrityCheck_Shown(object sender, EventArgs e)
        {
            CheckDocuIntegrity();
        }

        /// <summary>
        /// 
        /// </summary>
        private void CheckDocuIntegrity()
        {
            this.Enabled = false;
            if (gridIntegrity1.DataSource != null)
            {
                ((DataTable)gridIntegrity1.DataSource).Rows.Clear();
                ((DataTable)gridIntegrity2.DataSource).Rows.Clear();
            }
            lblStatus.Text = "In Arbeit...";
            Application.DoEvents();

            List<string> filesInXml = new List<string>();
            foreach (DataRow docuRow in DAO.Tables[typeof(Document)].Rows)
            {
                int docuId = docuRow[Document.Fields.Id].ToInt();

                filesInXml.AddRange(DAO.Tables[typeof(Page)].AsEnumerable().Where(row => row[Page.Fields.DocumentId].ToInt().Equals(docuId)).Select(row => ((Page)ElementBase.Instantiate(row)).CreateFileStoreName(docuRow)).ToList());
            }

            //List<string> filesInDir = Directory.GetFiles(@"I:\ATRIUM\DATEN\APPS\Haushalt\DataEugen\FileStore", "*.*", SearchOption.TopDirectoryOnly).Select(item => Path.GetFileName(item)).ToList();
            List<string> filesInDir = Directory.GetFiles(DAO.Settings.Selected.FileFullPath, "*.*", SearchOption.TopDirectoryOnly).Select(item => Path.GetFileName(item)).ToList();

            DataTable tblIntegrity1 = new DataTable();
            tblIntegrity1.Columns.Add("col1");
            DataTable tblIntegrity2 = new DataTable();
            tblIntegrity2.Columns.Add("col1");

            filesInXml.Except(filesInDir).Select(item=>tblIntegrity1.NewRow()[0]=item).ToList().ForEach(row=>tblIntegrity1.Rows.Add(row));
            filesInDir.Except(filesInXml).Select(item=>tblIntegrity2.NewRow()[0]=item).ToList().ForEach(row=>tblIntegrity2.Rows.Add(row));

            gridIntegrity1.DataSource = tblIntegrity1;
            gridIntegrity2.DataSource = tblIntegrity2;

            this.Enabled = true;
            lblStatus.Text = "Bereit";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            CheckDocuIntegrity();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
